package com.algorithm.hqy.practice;

import java.util.Comparator;

/**
 * @Classname Josephu
 * @Date 2019/9/18 14:12
 * @auther by hqy
 * 采用单循环链表解决约瑟夫出圈问题。
 */
public class Josephu {

    private Child first = null;

    public void initCircleLinkedList(int num) {
        Child curChild = null;
        for (int i = 1; i <= num; i++) {
            Child temp = new Child(i);
            if (i == 1) {
                first = temp;
                first.next = first;
                curChild = first;
            } else {
                curChild.next = temp;
                temp.next = first;
                curChild = temp;
            }

        }
    }

    public void show() {
        Child temp = first;
        while (true) {
            if (temp == null) {
                System.out.println("链表里么有值~~~~~~");
            }

            System.out.println(temp);
            temp = temp.next;
            if (temp == first) {
                break;
            }
        }
    }


    public void printChildSort(int startNo, int countNo) {

        Child helper = first;

        Child cursor = first;
        while (helper.next != first) {
            helper = helper.next;
        }


        for (int i = 0; i < startNo - 1; i++) {
            helper = helper.next;
            cursor = cursor.next;
        }

        while (true) {

            for (int i = 0; i < countNo - 1; i++) {
                helper = helper.next;
                cursor = cursor.next;
            }
            if (cursor == helper) {
                System.out.println("当前出列的孩子是:" + cursor);
                break;
            }
            System.out.println("当前出列的孩子是:" + cursor);
            //cursor后移
            cursor = cursor.next;
            helper.next = cursor;


        }


    }

}

class Child {

    public Child next;

    private int no;

    public Child(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return this.getClass().getName() + ",no:" + this.no;
    }
}
